//
//  UITextField+Ext.swift
//  TTCOM-iOS
//
//  Created by Toj on 10/26/22.
//


import UIKit

public extension UITextField {
    
    var tf_isEmpty: Bool {
        return text == nil || text?.count == 0
    }
    
    /**
     设置占位文字的颜色
     - parameter color: 颜色
     */
    func tf_placeholderTextColor(color: UIColor) {
        setValue(color, forKeyPath: "_placeholderLabel.textColor")
    }
    
    /**
     左面间隔
     - parameter width: 左边的间隙
     */
    func tf_leftMargin(width: CGFloat) {
        leftViewMode = .always
        let newLeftView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: frame.size.height))
        newLeftView.backgroundColor = UIColor.clear
        leftView = newLeftView
    }
}

public extension UITextField {
    
    func tf_selectedTextIndex(offset: Int) {
        guard let newPos = position(from: beginningOfDocument, offset: offset) else { return }
        selectedTextRange = textRange(from: newPos, to: newPos)
    }
    
    func tf_delete(shouldChangeCharactersIn range: NSRange, formatType: String.SPFormatterType = .threeFourFour, separator: String) -> Bool {
        
        let length = range.length
        if length == 0 { return true }
        
        let location = range.location
        
        var textStr = text ?? ""
        var textCount = textStr.count
        // 删除光标前最后一个字符, 如果是选中某些字符则删除选中部分
        deleteBackward()
        
        // 删除1个字符
        if length == 1 {
            // 从最后删除
            if location == textCount - 1 {
                textStr = text ?? ""
                textCount = textStr.count
                // 删没了
                if textCount == 0 { return false }
                
                // 最后一个字符是占位字符
                if textStr.cm_lastCharString == separator {
                    deleteBackward()
                }
                return false
            }
        }
        
        // 从中间删除
        textStr = text ?? ""
        text = textStr.cm_numberFormat(format: formatType)
        // 重置光标位置 = 当前光标位置
        tf_selectedTextIndex(offset: location)
        return false
    }
    
    // 粘贴有问题格式化后会有偏差, 设置的off=9表现在第8个位置, 可以把粘贴禁用
    func tf_insert(shouldChangeCharactersIn range: NSRange, replacementString string: String, maxLength: Int, formatType: String.SPFormatterType = .threeFourFour, separator: String) -> Bool {
        
        // 当前位置
        let location = range.location
        
        // 输入前: 内容
        var textStr = text ?? ""
        let totalString = textStr.to_cocoaString.replacingCharacters(in: range, with: string)
        let originalString = totalString.replacingOccurrences(of: separator, with: "")
        var locationAfterString = totalString
        // 输入内容大于最大长度
        if maxLength > 0, originalString.count > maxLength {
            textStr = originalString.cca_subsring(to: maxLength)
        }
        else {
            insertText(string)
            textStr = text ?? ""
            locationAfterString = textStr
        }
        // 偏移量 = 当前光标位置 + 输入字符数量
        var offset = location + string.count
        let offSetString = locationAfterString.cca_subsring(to: offset).cm_numberFormat(format: formatType)
        offset = offSetString.count
        text = textStr.cm_numberFormat(format: formatType)
        
        tf_selectedTextIndex(offset: offset)
        
        return false
    }
}
